<?php

function PasienKode() {
	$CI = get_instance();
	$getKodeMax = $CI->registrasi_model->getPasienKode();
	
	$rmKode = '001';
	if ( !empty($getKodeMax) ) {
		$kodeMax = str_replace('RM', '', $getKodeMax);
		$kodeMax = $kodeMax + 1;
		$rmKode = substr('00'.$kodeMax, -3);	
	}

	$kode = 'RM'.$rmKode;
	return $kode;
}

function RMKode() {
	$CI = get_instance();
	$getKodeMax = $CI->registrasi_model->getRMKode();
	
	$rmKode = '001';
	if ( !empty($getKodeMax) ) {
		$kodeMax = str_replace('RJ', '', $getKodeMax);
		$kodeMax = $kodeMax + 1;
		$rmKode = substr('00'.$kodeMax, -3);	
	}

	$kode = 'RJ'.$rmKode;
	return $kode;
}

function screenDetect() {
    $CI = get_instance();
    if (!isset($CI->session->screenWidth))
        return false;
    if (!isset($CI->session->screenHeigh))
        return false;
    return true;
}
function initWidthHeigh() {
    $CI = get_instance();
    if (!isset($CI->session->screenWidth)) {
        $CI->session->screenWidth = 1024;
    }
    if (!isset($CI->session->screenHeigh)) {
        $CI->session->screenHeigh = 768;
    }

    $CI->session->defaultScreenWidth = 1024;
    $CI->session->defaultScreenHeight = 768;
    
    $CI->session->defaultLeftWidth = 790;
    $CI->session->defaultRightWidth = 205;
    $CI->session->defaultHeight = 690;
    $CI->session->leftWidth =  round(($CI->session->defaultLeftWidth / $CI->session->defaultScreenWidth) * $CI->session->screenWidth);
    $CI->session->rightWidth =  round(($CI->session->defaultRightWidth / $CI->session->defaultScreenWidth) * $CI->session->screenWidth);
    $CI->session->height =  round(($CI->session->defaultHeight / $CI->session->defaultScreenHeight) * $CI->session->screenHeigh);
}
function getLeftWidth() {
    $CI = get_instance();
    return $CI->session->leftWidth;
}
function getRightWidth($min = 0) {
    $CI = get_instance();
    return ($CI->session->rightWidth - $min );
}
function getDeviceWidth ($width) {
    $CI = get_instance();
    $width = round ( ($width / $CI->session->defaultLeftWidth) * $CI->session->leftWidth ) ;
    return $width; 
}
function getHeight() {
    $CI = get_instance();
    return $CI->session->height;
}

function updateDashboardConfig($config) {
    $file = FCPATH . 'data' . DS . 'var' . DS . 'config' . DS . 'dashboard';
    return @file_put_contents($file,serialize($config)); 
}
function getDashboardConfig() {
    $file = FCPATH . 'data' . DS . 'var' . DS . 'config' . DS . 'dashboard';
    if (!file_exists($file)) {
        return array('tipe'=>'all','list'=>array());
    }
    $config = file_get_contents($file);
    if ($config) {
        $config = unserialize($config);
        return $config;
    }
    return array('tipe'=>'all','list'=>array());
}

function getIpcalc($ip) {
    $_t = explode ( '/', $ip );
    if (count ( $_t ) != 2)
        return array ();
    $_ip = `/bin/ipcalc -m -n -b -p -4 $ip`;
    $ipresult = array ();
    $ipresult ['IPADDRESS'] = $_t [0];
    $ipwan = explode ( PHP_EOL, $_ip );
    foreach ( $ipwan as $v ) {
        $v = trim ( $v );
        if (! empty ( $v )) {
            $t = explode ( '=', $v );
            $ipresult [$t [0]] = $t [1];
        }
    }
    $NETWORK = explode ( '.', $ipresult ['NETWORK'] );
    $NETWORK [3] = $NETWORK [3] + 1;
    $ipresult ['GATEWAY'] = implode ( '.', $NETWORK );
    return $ipresult;
}

function convert_ber($value) {
	if( !$value )
		return '-';

	$length = substr_count($value, '0');
	
	$val = substr($value, -2, $length);
	if($val > 10)
		$val = ($val / 10); 
	else
		$val = substr($val, -1);
		

	$ber = $value;//$val.'.10<sup>-'.$length.'</sup>';
	return $ber;
}

function convert_modcod($value) {
	if( !$value && !is_int($value) )
		return '-';
		
	$modcod = substr($value, -4);
	return $modcod;	
}

function get_image($path) {
	if( !$path )
		return '';
		
	$image = '<img src="'.$path.'" />';	
	return $image;
}

function status_ebno($data) {
	if( !$data || !is_array($data) )
		return '';
		
	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['EBNo']) ? $config['EBNo'] : '';

	$value = isset($data['modemStatusEBNo']) ? $data['modemStatusEBNo'] : '';
	return $value;
}

function status_ber($data) {
	if( !$data || !is_array($data) )
		return '';
		
	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['BER']) ? $config['BER'] : '';

	$value = isset($data['modemStatusBER']) ? $data['modemStatusBER'] : '';
	return $value;
}

function status_temperature($data) {
	if( !$data || !is_array($data) )
		return '';
		
	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['Temperatur']) ? $config['Temperatur'] : '';

	$value = strlen($data['modemStatusTemperatur']) > 0 ? $data['modemStatusTemperatur'].' <sup>o</sup>C' : '';
	return $value;
}

function status_ratio($data) {
	if( !$data || !is_array($data) )
		return '';
		
	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['Ratio']) ? $config['Ratio'] : '';

	$value = strlen($data['modemStatusCnCRatio']) > 0 ? $data['modemStatusCnCRatio'].' dB' : '';
	return $value;
}

function get_modcod($value) {
	if( !$value )
		return '';
	
	$modcod = substr($value, -4);		
	return $modcod;	
}

function get_modcod_value($value) {
	if( !$value )
		return '';

	$val = get_modcod($value);	
	$data = explode('/', $val); 	
	$top = isset($data[0]) ? trim($data[0]) : '';
	$bottom = isset($data[1]) ? trim($data[1]) : '';
	
	$modcod = '';
	if($top && $bottom)
		$modcod = ($top / $bottom);
	
	return $modcod;	
}

function status_modcod($data) {
	if( !$data || !is_array($data) )
		return '';
		
	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['ModCod']) ? $config['ModCod'] : '';

	$value_origin = isset($data['modemStatusModCod']) ? $data['modemStatusModCod'] : '';
	$value = get_modcod_value($value_origin);
	
	$string = get_modcod($value_origin);
	return $string;
}

function get_delay($value) {
	if( !$value )
		return '';
	
	$val = str_replace('usec', '', $value);
	$val = str_replace(' ', '', $val);
	
	$delay = ceil($val / 1000);
	
	return $delay;	
}

function status_delay($data) {
	if( !$data || !is_array($data) )
		return '';

	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['Delay']) ? $config['Delay'] : '';

	$value_origin = isset($data['modemStatusCnCDelay']) ? $data['modemStatusCnCDelay'] : '';
	$value = get_delay($value_origin);
	$value = $value ? $value.' ms' : '';
	
	return $value;
}

function status_buffer($data) {
	if( !$data || !is_array($data) )
		return '';

	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['Buffer']) ? $config['Buffer'] : '';

	$value = isset($data['modemStatusBuffer']) ? $data['modemStatusBuffer'] : '';
	return $value;
}

function get_netstat_class($data) {
	if( !$data || !is_array($data) )
		return '';
		
	$CI =& get_instance();
	$config =  $CI->config->item('status');
	$status = !empty($config['EBNo']) ? $config['EBNo'] : '';

	$value = isset($data['modemStatusEBNo']) ? $data['modemStatusEBNo'] : '';
	
	$image = 'notactive';
	if( $status && $value ) {
		$normal = !empty($status['normal']) ? $status['normal'] : '';
		$down = !empty($status['down']) ? $status['down'] : '';
		
		if( !empty($normal) && !empty($down) ) {
			//Up
			if ( $value >= $normal ) {
				$image = 'ok';
				
			//Low	
			} elseif ( $value < $normal && $value > $down) {
				$image = 'low';
				
			//Down	
			} elseif ( $value <= $down ) {
				$image = 'drop';
			}
		}
	}
	
	return $image;
}
